<template>
  <div id="app">
    <!--页面导航栏-->
    <Header></Header>

    <!--页面主体部分-->
    <div class="all">
      <router-view/>
    </div>

  </div>
</template>

<script>
  import Header from "@/components/header/Header"
  import {getCookie} from "./tools/cookie";

  export default {
    name: "APP",
    components: {
      Header
    },
    data() {
      return {}
    },
    methods: {
      /* 我要学Java*/
      wyxJava: function () {
        console.log(`欢迎合伙人加入`);
        console.log(`微信:152 - 5192 - 2665‬`);
      },
      //请求拦截器
      requestConfig:function () {
        this.axios.interceptors.request.use(
          (config) => {
            const sessionToken = window.sessionStorage.getItem("token");
            //如果 token 不为空
            if (sessionToken != null) {
              config.headers.Authorization = sessionToken
            }
            //post请求
            if (config.method === "post" || config.method === "put") {
              config.headers["Content-Type"] = "application/json;charset=UTF-8"
              //替代 URLSearchParams
              // config.data = this.qs.stringify(config.data, {arrayFormat: 'indices', allowDots: true});
            }
            return config;
          },
          (error) => {
            return Promise.reject(error);
          })
      },
      //响应拦截器
      responseConfig:function () {
        this.axios.interceptors.response.use(
          (resp) => {
            let data = resp.data;
            if (data.code == 500) {
              this.$Notice.error({
                title: "服务器正在维护...",
                desc:"部分功能不可用"
              });
            }
            //响应正确时
            return resp;
          },
          (error) => {
            return Promise.reject(error);
          }
        )
      },
      init: function () {
        //是否登录
        const isLogin = getCookie("isLogin");
        //如果记住密码
        if (isLogin) {
          //取出信息，存到 sessionStorage
          let cookieToken = getCookie("token");
          let cookieUserInfo = getCookie("userInfo");
          window.sessionStorage.setItem("isLogin", true);
          window.sessionStorage.setItem("token", cookieToken);
          window.sessionStorage.setItem("userInfo", cookieUserInfo);
        }
        //从sessionStorage取出信息，放到全局
        const isLoginSession = window.sessionStorage.getItem("isLogin");
        if (isLoginSession) {
          this.Store.commit("setIsLogin",true);
          this.Store.commit("setToken",window.sessionStorage.getItem("token"));
          this.Store.commit("setUserInfo",JSON.parse(window.sessionStorage.getItem("userInfo")));
        }
      }
    },
    mounted() {
      this.wyxJava();
      //拦截器
      this.requestConfig();
      this.responseConfig();
      //加载当前
      this.init();
      //监听，刷新 app 组件
      this.Bus.$on("my-event-app-load", (data) => {
        this.init();
      });
    }
  }
</script>

<style>
  * {
    margin: 0px;
    padding: 0px;
    text-decoration: none; /*下划线*/
    list-style: none; /* ul li 的小圆点*/
    font-family: Arial; /*字体*/
    font-size: 16px; /*文字大小*/
  }


  #app .all {
    /*限定各个路由的宽度*/
    /*width: 80%;*/
    /*min-width: 1120px;*/
    min-height: calc(100vh - 70px);
    margin: 0 auto;
  }
</style>
